<template>
  <!-- 还款记录 -->
  <div class="ContactPerson">
    <div class="searchNav">
      <!-- <h2>联系人</h2> -->
      <!-- <el-button
        class="button"
        icon="el-icon-plus"
        type="primary"
        style="margin-left: 0"
        @click="handleAdd"
        >新增</el-button
      > -->
      <div class="searchParams">
        <div class="block">
          <el-date-picker
            v-model="range"
            type="daterange"
            style="width: 250px; margin-left: 10px"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="changeTime"
          >
          </el-date-picker>
        </div>
        <el-input
          class="input"
          v-model="queryParam.Keyword"
          placeholder="请输入赊货方"
          clearable
          @keydown.enter.native="search"
        ></el-input>
        <el-button class="button" @click="search" type="primary"
          >查询</el-button
        >
        <el-button class="button" @click="reset('reset')">重置</el-button>
      </div>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%" :max-height="height">
        <el-table-column prop="BuyerName" label="赊货方">
          <template slot-scope="scope">
            <el-link @click="handlelDetail(scope.row)">{{
              scope.row.BuyerName
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="RepayTime" label="还款日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.RepayTime ? scope.row.RepayTime.substring(0, 10) : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="CreateTime" label="赊货日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.CreateTime ? scope.row.CreateTime.substring(0, 10) : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ReceiveSum" label="赊货金额">
          <template slot-scope="scope">
            <span>{{ toThousands(scope.row.TotalSum) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="IsRepay" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsRepay == true" type="primary"
              >已还款</el-tag
            >
            <el-tag v-else type="warning">待还款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="!scope.row.IsRepay"
              type="text"
              @click="handleRepayment(scope.row)"
              style="color: #409eff"
              >还款</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="display: flex; justify-content: flex-end; margin-top: 8px">
        <pagination
          :total="pagination.total"
          :page.sync="pagination.PageIndex"
          :limit.sync="pagination.PageRows"
          @pagination="handlePageChange"
        />
      </div>
    </div>
    <!-- 还款 -->
    <el-dialog
      title="还款"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div>
        <!--        <span>还款金额: {{ toThousands(RepaymentMoney) }}</span>-->
        <el-input
          disabled
          placeholder="请输入还款金额"
          v-model="RepaymentMoney"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitButtonRepayment"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <EditForm ref="editForm" :parentObj="{ ...this }" />

    <!-- 资质详情 -->
    <MyMerchantDetail ref="MyMerchantDetail" :parentObj="{ ...this }" />
  </div>
</template>

<script>
import radioGroup from "@/components/CompanyRadioGroup.vue";
import { toThousands } from "@/utils/Tools";
import EditForm from "./PaymentHistoryForm.vue";

// 引入资质详情组件 统一用的是我的商户页面里的资质详情
import MyMerchantDetail from "../MyMerchant/MyMerchantDetail.vue";
export default {
  props: {},
  components: {
    EditForm,
    radioGroup,
    MyMerchantDetail,
  },
  mounted() {
    this.getDebtrecords();
  },
  created() {},
  computed: {
    height() {
      return document.documentElement.clientHeight * 0.75;
    },
  },
  data() {
    return {
      // 还款绑定字段
      RepaymentMoney: "",
      // 整体页面加载
      fullscreenLoading: false,
      // 时间范围
      range: [],
      // 还款弹窗
      dialogVisible: false,
      // 时间范围
      range: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        pageSizeOptions: [10, 50, 100],
      },
      sorter: {
        field: "CreateTime",
        order: "desc",
      },
      // 还款金额
      repaymentMoney: null,
      tableData: [],
      queryParam: {
        // 关键词搜索
        Keyword: "",
        StartDate: "",
        EndDate: "",
      },
      loading: false,
      toThousands,
      //   当前行
      row: {},
    };
  },
  methods: {
    // 商铺资质详情跳转
    handlelDetail(row) {
      console.log("资质详情", row);
      if (row.BuyerStoreId) {
        this.$refs.MyMerchantDetail.openForm(row.BuyerStoreId);
      } else {
        this.$message.error("没有店铺信息");
      }
    },
    // 获取还款记录列表数据
    getDebtrecords() {
      let userId = JSON.parse(sessionStorage.getItem("userInfo"));
      this.$http
        .post(`/Base/Bank_Info/Getuserids?Id=${[userId.Id]}`)
        .then((res) => {
          console.log("获取第一个数据", res);
          if (res.Success) {
            this.getDataList(res.Data);
          }
        });
    },
    // 还款
    submitButtonRepayment() {
      var that = this;
      that.loading = true;
      if (this.RepaymentMoney === "") {
        this.$message.error("请输入还款金额");
        return;
      }
      this.$http
        .post("/Base/Bank_Info/OrderRepayment", {
          Id: this.row.Id,
        })
        .then((resJson) => {
          //请求结束关闭loading
          setTimeout(function () {
            that.loading = false;
          }, 600);
          if (resJson.Success) {
            this.$message.success("已还款");
            //   关闭弹窗  更新列表
            // 关闭还款弹窗
            this.dialogVisible = false;
            // 获取还款记录列表数据
            this.getDebtrecords();
          } else {
            this.$message.error();
          }
        });
      // this.$message.success("已还款");
    },
    // 开始结束时间选择
    changeTime(e) {
      this.range = e;
      this.queryParam.StartDate = this.range[0];
      this.queryParam.EndDate = this.range[1];
      // 刷新放款列表
      this.tableData = [];
      this.pagination.current = 1;
      this.getDebtrecords();
    },
    // 参数重置
    reset(event) {
      if (event === "reset") {
        this.range = [];
        this.queryParam.Keyword = "";
        this.range = [];
        this.queryParam.StartDate = "";
        this.queryParam.EndDate = "";
      }
      this.tableData = [];
      this.pagination.current = 1;
      this.getDebtrecords();
    },
    // 搜索
    search() {
      this.reset();
    },
    // 新增
    handleAdd() {
      this.$refs.editForm.openForm();
    },
    // 还款
    handleRepayment(row) {
      console.log("还款", row);
      this.row = row;
      this.RepaymentMoney = row.TotalSum;
      // 还款弹窗
      this.dialogVisible = true;
    },
    // 分页函数
    handlePageChange(data) {
      this.pagination.current = data.pageIndex;
      this.pagination.pageSize = data.pageSize;
      this.getDebtrecords(); //这是重新获取修改日志列表
    },
    // 获取还款记录列表数据
    getDataList(data) {
      var that = this;
      //创建loading对象开始遮罩
      this.loading = true;
      this.$http
        .post("/Base/Bank_Info/Debtrecords", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || "Id",
          SortType: this.sorter.order,
          Search: {
            ...this.queryParam,
            userids: data,
          },
        })
        .then((resJson) => {
          //请求结束关闭loading
          setTimeout(function () {
            that.loading = false;
          }, 600);
          if (resJson.Success) {
            this.tableData = resJson.Data;
            if (this.tableData) {
              const pagination = { ...this.pagination };
              pagination.total = resJson.Total;
              this.pagination = pagination;
            }
          }
        });
    },
    // 关闭弹窗
    handleClose(done) {
      done();
    },
  },
  watch: {},
};
</script>

<style lang="less" scoped></style>
